package com.ruoyi.jiaopei.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.jiaopei.domain.TbCurriculumStudent;
import com.ruoyi.jiaopei.domain.TbKj;
import com.ruoyi.jiaopei.domain.vo.DayCurriculumVo;
import com.ruoyi.jiaopei.mapper.TbCurriculumStudentMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.jiaopei.domain.bo.TbCurriculumBo;
import com.ruoyi.jiaopei.domain.vo.TbCurriculumVo;
import com.ruoyi.jiaopei.domain.TbCurriculum;
import com.ruoyi.jiaopei.mapper.TbCurriculumMapper;
import com.ruoyi.jiaopei.service.ITbCurriculumService;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 课程Service业务层处理
 *
 * @author macy
 * @date 2024-04-03
 */
@RequiredArgsConstructor
@Service
public class TbCurriculumServiceImpl implements ITbCurriculumService {

    private final TbCurriculumMapper baseMapper;
    private final TbCurriculumStudentMapper curriculumStudentMapper;


    /**
     * 查询课程
     */
    @Override
    public TbCurriculumVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    @Override
    public Long selectCount(Wrapper<TbCurriculum> queryWrapper) {
        return baseMapper.selectCount(queryWrapper);
    }

    /**
     * 查询课程列表
     */
    @Override
    public TableDataInfo<TbCurriculumVo> queryPageList(TbCurriculumBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TbCurriculum> lqw = buildQueryWrapper(bo);
        Page<TbCurriculumVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询课程列表
     */
    @Override
    public List<TbCurriculumVo> queryList(TbCurriculumBo bo) {
        LambdaQueryWrapper<TbCurriculum> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TbCurriculum> buildQueryWrapper(TbCurriculumBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TbCurriculum> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), TbCurriculum::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), TbCurriculum::getType, bo.getType());
        return lqw;
    }

    /**
     * 新增课程
     */
    @Override
    public Boolean insertByBo(TbCurriculumBo bo) {
        TbCurriculum add = BeanUtil.toBean(bo, TbCurriculum.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改课程
     */
    @Override
    public Boolean updateByBo(TbCurriculumBo bo) {
        TbCurriculum update = BeanUtil.toBean(bo, TbCurriculum.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TbCurriculum entity){
        if(null==entity.getId()){
            entity.setCreatedDate(new Date());
        }
    }

    /**
     * 批量删除课程
     */
    @Override
    public Boolean deleteWithValidByIds(List<Long> ids, Boolean isValid) {
        if(isValid){
            for (int i = 0; i <ids.size() ; i++) {
                long count=curriculumStudentMapper.selectCount(new LambdaQueryWrapper<TbCurriculumStudent>()
                    .eq(TbCurriculumStudent::getCurriculumId,ids.get(i))
                );

                if(count>0){
                    throw new ServiceException("课程下还有关联的学员，不能删除。");
                }

            }
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }


    @Override
    public List<DayCurriculumVo> queryDayList() {
        return baseMapper.queryDayList();
    }
}
